package com.trxy.hotelsystem.contorller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.trxy.hotelsystem.model.clientInf;
import com.trxy.hotelsystem.model.floor;
import com.trxy.hotelsystem.model.houseType;
import com.trxy.hotelsystem.model.room;
import com.trxy.hotelsystem.service.impl.FloorserviceImpl;
import com.trxy.hotelsystem.service.impl.RoomserviceImpl;
import com.trxy.hotelsystem.service.impl.RoomtypeServiceImpl;

import frame.uitls.PrimaryKeyUtil;
import net.sf.json.JSONArray;

@Controller
public class RoomController {
	
	@Autowired
	private RoomserviceImpl RoomServiceImpl;
	
	@Autowired
	private FloorserviceImpl floorserviceImpl;
	
	@Autowired
	private RoomtypeServiceImpl RoomtypeserviceImpl;
	
	//查询所有房间动态
	@RequestMapping(value = "queryRoom.do")
	public String queryRoom(HttpServletRequest request, HttpServletResponse response, 
			Model model,room rom,houseType housetype,
			@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize) throws Exception {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//开启分页查询
		PageHelper.startPage(pageNum, pageSize);
		
		List<room> list = RoomServiceImpl.queryRoomAll();
		List<floor> floorlist = floorserviceImpl.queryFloorAll();
		List<houseType> houselist = RoomtypeserviceImpl.queryHouseAll();
		
        PageInfo<room> page = new PageInfo<>(list);
		
		model.addAttribute("pageInfo", page);
		model.addAttribute("floorlist",floorlist);
		model.addAttribute("houselist",houselist);
		
		
		return "view/room/room";
	}
	
	//查询所有房间动态---退房界面
		@RequestMapping(value = "queryRoom2.do")
		public String queryRoom2(HttpServletRequest request, HttpServletResponse response, 
				Model model,room rom,houseType housetype,
				@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize) throws Exception {
			
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			
			//开启分页查询
			PageHelper.startPage(pageNum, pageSize);
			
			rom.setStatus("使用中");
			
			List<room> list = RoomServiceImpl.selectRoomByother(rom);
//			List<room> list = RoomServiceImpl.queryRoomAll();
			List<floor> floorlist = floorserviceImpl.queryFloorAll();
			List<houseType> houselist = RoomtypeserviceImpl.queryHouseAll();
			
	        PageInfo<room> page = new PageInfo<>(list);
			
			model.addAttribute("pageInfo", page);
			model.addAttribute("floorlist",floorlist);
			model.addAttribute("houselist",houselist);
			
			
			return "view/checkin/checkout";
		}

		//查询所有房间动态---会员房态表
		@RequestMapping(value = "queryRoom3.do")
		public String queryRoom3(HttpServletRequest request, HttpServletResponse response, 
				Model model,room rom,houseType housetype,
				@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize) throws Exception {
			
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			
			//开启分页查询
			PageHelper.startPage(pageNum, pageSize);
			
			List<room> list = RoomServiceImpl.queryRoomBystaus("空闲");
			List<floor> floorlist = floorserviceImpl.queryFloorAll();
			List<houseType> houselist = RoomtypeserviceImpl.queryHouseAll();
			
	        PageInfo<room> page = new PageInfo<>(list);
			
			model.addAttribute("pageInfo", page);
			model.addAttribute("floorlist",floorlist);
			model.addAttribute("houselist",houselist);
			
			
			return "view/vipview/viproom";
		}
	
	//通过房间号查询房间动态
	@RequestMapping(value = "selectRoomByNumber.do")
	public void selectRoomByNumber(HttpServletRequest request, HttpServletResponse response, 
			Model model,room rom) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		
		String roomNumber = request.getParameter("roomNumber");
		
		rom.setRoomnum(Integer.parseInt(roomNumber));
		rom.setStatus("使用中");
		
		List<room> list = RoomServiceImpl.selectRoomByNumber3(rom);

		String st = JSONArray.fromObject(list).toString();
		response.getWriter().write(st);
		
	}
	
	//通过楼层、房间动态、房型条件查询房间信息
	@RequestMapping(value = "/selectRoomByother.do")
	public String selectRoomByother(HttpServletRequest request, HttpServletResponse response, 
			Model model,room rom,
			@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		
		//开启分页查询
		PageHelper.startPage(pageNum, pageSize);
		
		String floor = request.getParameter("floor");
		String state = request.getParameter("state");
		String houseType = request.getParameter("houseType");
		String floor2 = floor.trim();
		String state2 = state.trim();
		String houseType2 = houseType.trim();
		
		if(floor2.equals("默认")) {
			rom.setFloor(null);
		}else {
			rom.setFloor(floor2);
		}
		
		if(state2.equals("默认")) {
			rom.setStatus(null);
		}else {
			rom.setStatus(state2);
		}
		
		if(houseType2.equals("默认")) {
			rom.setType(null);
		}else {
			rom.setType(houseType2);
		}
		
	
		List<room> list = RoomServiceImpl.selectRoomByother(rom);
		
		 PageInfo<room> page = new PageInfo<>(list);
			
		model.addAttribute("pageInfo", page);
		
		model.addAttribute("floor",floor);
		model.addAttribute("state",state);
		model.addAttribute("houseType",houseType);
		model.addAttribute("inf", "多条件");
		
		return "view/room/room";
		
	}
	
	//通过楼层、房间动态、房型条件查询房间信息------退房
	@RequestMapping(value = "/selectRoomByother2.do")
	public String selectRoomByother2(HttpServletRequest request, HttpServletResponse response, 
			Model model,room rom,
			@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		
		//开启分页查询
		PageHelper.startPage(pageNum, pageSize);
		
		String floor = request.getParameter("floor");
		String state = request.getParameter("state");
		String houseType = request.getParameter("houseType");
		String floor2 = floor.trim();
		String state2 = state.trim();
		String houseType2 = houseType.trim();
		
		if(floor2.equals("默认")) {
			rom.setFloor(null);
		}else {
			rom.setFloor(floor2);
		}
		
		if(state2.equals("默认")) {
			rom.setStatus(null);
		}else {
			rom.setStatus(state2);
		}
		
		if(houseType2.equals("默认")) {
			rom.setType(null);
		}else {
			rom.setType(houseType2);
		}
		
	
		List<room> list = RoomServiceImpl.selectRoomByother(rom);
		
		 PageInfo<room> page = new PageInfo<>(list);
			
		model.addAttribute("pageInfo", page);
		
		model.addAttribute("floor",floor);
		model.addAttribute("state",state);
		model.addAttribute("houseType",houseType);
		model.addAttribute("inf", "多条件");
		
		return "view/checkin/checkout";
		
	}
	
	
	//通过楼层、房间动态、房型条件查询房间信息
		@RequestMapping(value = "/selectRoomByother3.do")
		public String selectRoomByother3(HttpServletRequest request, HttpServletResponse response, 
				Model model,room rom,
				@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="5")Integer pageSize) throws IOException {
			
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			
			
			//开启分页查询
			PageHelper.startPage(pageNum, pageSize);
			
			String floor = request.getParameter("floor");
			String state = "空闲";
			String houseType = request.getParameter("houseType");
			String floor2 = floor.trim();
			String houseType2 = houseType.trim();
			
			if(floor2.equals("默认")) {
				rom.setFloor(null);
			}else {
				rom.setFloor(floor2);
			}
			
			rom.setStatus(state);
			
			if(houseType2.equals("默认")) {
				rom.setType(null);
			}else {
				rom.setType(houseType2);
			}
			
		
			List<room> list = RoomServiceImpl.selectRoomByother(rom);
			
			 PageInfo<room> page = new PageInfo<>(list);
				
			model.addAttribute("pageInfo", page);
			
			model.addAttribute("floor",floor);
			model.addAttribute("state",state);
			model.addAttribute("houseType",houseType);
			model.addAttribute("inf", "多条件");
			
			return "view/vipview/viproom";
			
		}
		
		//通过楼层、房间动态、房型条件查询房间信息
				@RequestMapping(value = "/selectRoomByother4.do")
				public void selectRoomByother4(HttpServletRequest request, HttpServletResponse response, 
						Model model,room rom) throws IOException {
					
					request.setCharacterEncoding("utf-8");
					response.setContentType("text/html;charset=utf-8");
					
					
					String floor = request.getParameter("floor");
					String state = request.getParameter("state");
					String houseType = request.getParameter("houseType");
					String floor2 = floor.trim();
					String state2 = state.trim();
					String houseType2 = houseType.trim();
					
					if(floor2.equals("默认")) {
						rom.setFloor(null);
					}else {
						rom.setFloor(floor2);
					}
					
					if(state2.equals("默认")) {
						rom.setStatus(null);
					}else {
						rom.setStatus(state2);
					}
					
					if(houseType2.equals("默认")) {
						rom.setType(null);
					}else {
						rom.setType(houseType2);
					}
					
				
					List<room> list = RoomServiceImpl.selectRoomByother(rom);
					//JSON格式返回数据
					String st = JSONArray.fromObject(list).toString();
					response.getWriter().write(st);	
					
				}
		
		//通过房间动态和会员账号查询自己使用中的房间
		@RequestMapping(value = "myselfUseingRoom.do")
		public void myselfUseingRoom(HttpServletRequest request,HttpServletResponse response,
				HttpSession session,room room) throws IOException {
			
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			
			//获取session值
			clientInf us = (clientInf) session.getAttribute("viplogin");
			
			room.setReserved2(us.getNumber());
			room.setStatus("使用中");
			
			List<room> list = RoomServiceImpl.selectMyselfRoom(room);
			
			//JSON格式返回数据
			String st = JSONArray.fromObject(list).toString();
			response.getWriter().write(st);	
			
		}
		
		//查询楼层
		@RequestMapping(value = "selectFloor.do")
		public String selectFloor(HttpServletRequest request, HttpServletResponse response,
				Model model) {
			
			List<floor> floorlist = floorserviceImpl.queryFloorAll();
			
			model.addAttribute("floorlist",floorlist);
			
			return "view/System/floor";
		}
		
		//删除楼层
		@RequestMapping(value = "deleteFloor.do")
		public void deleteFloor(HttpServletRequest request,HttpServletResponse response,
				floor flor) throws IOException {
			
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			
			String flr = request.getParameter("floor");
			
			PrintWriter pw = response.getWriter();
			
			int i = floorserviceImpl.deleteFloor(flr);
			
			if(i > 0) {
				pw.write("1");
			}else {
				pw.write("0");
			}
		}
		
		//添加楼层
		@RequestMapping(value = "addFloor.do")
		public void addFloor(HttpServletRequest request,HttpServletResponse response,
				floor flor) throws IOException {
			
			PrintWriter pw = response.getWriter();
			
			List<floor> floorlist = floorserviceImpl.queryFloorAll();
			
			String fo = request.getParameter("floor");
			
			int j = 3;
			
			for(floor one:floorlist) {
				if(one.getFloor().equals(fo)) {
					j = 2;
				}
				
			}
			
			if(j == 2) {
				pw.write("2");
			}else {
				flor.setFloor(fo);
				
				int i = floorserviceImpl.insertFloor(flor);
				
				if(i > 0) {
					pw.write("1");
				}else {
					pw.write("0");
				}
			}
		}
		
		
		//房间管理
		@RequestMapping(value = "roommanagement.do")
		public String roommanagement(HttpServletRequest request,HttpServletResponse response,
				floor flor,houseType housetype,room rom,Model model
				) throws UnsupportedEncodingException {
			
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			
//			//开启分页查询
//			PageHelper.startPage(pageNum, pageSize);
			
			List<room> list = RoomServiceImpl.queryRoomAll();
			List<floor> floorlist = floorserviceImpl.queryFloorAll();
			List<houseType> houselist = RoomtypeserviceImpl.queryHouseAll();
			
//	        PageInfo<room> page = new PageInfo<>(list);
			
			model.addAttribute("pageInfo", list);
			model.addAttribute("floorlist",floorlist);
			model.addAttribute("houselist",houselist);
			
			return "view/System/roommanagement";
		}
		
		//添加房间号
		@RequestMapping(value = "addRoom.do")
		public void addRoom(HttpServletRequest request,HttpServletResponse response,
				floor flor,houseType housetype,room rom) throws IOException {
			
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			
			String roomNumber = request.getParameter("roomNumber");
			String floor = request.getParameter("floor");
			String roomtype = request.getParameter("roomtype");
			
			PrintWriter pw = response.getWriter();
			
			List<room> list = RoomServiceImpl.queryRoomAll();
			
			int j = 3;
			
			//检查房间是否已存在
			for(room one:list) {
				if(one.getRoomnum() == Integer.parseInt(roomNumber)) {
					j = 2;
				}
			}
			
			String price = null;
			int quantity = 0;
			List<houseType> houselist = RoomtypeserviceImpl.queryHouseAll();
			for(houseType one:houselist) {
				if(one.getHousetype().equals(roomtype)) {
					price = one.getReserved1();
					quantity = one.getGalleryful();
				}
			}
			
			if(j == 2) {
				pw.write("2");
			}else {
				
				rom.setRoomnum(Integer.parseInt(roomNumber));
				rom.setFloor(floor);
				rom.setPrice(Double.parseDouble(price));
				rom.setType(roomtype);
				rom.setQuantity(quantity);
				rom.setStatus("空闲");
				
				//插入房间
				int i = RoomServiceImpl.insertSelective(rom);
				if(i > 0) {
					pw.write("1");
				}else {
					pw.write("0");
				}
			}	
		}
		
		//删除房间
		@RequestMapping(value = "deleteRoom.do")
		public void deleteRoom(HttpServletRequest request,HttpServletResponse response,
				room rom) throws IOException {
			
			PrintWriter pw = response.getWriter();
			
			String roomNumber = request.getParameter("roomNumber");
			
			int i = RoomServiceImpl.deleteRoomByroomNumber(Integer.parseInt(roomNumber));
			
			if(i > 0) {
				pw.write("1");
			}else {
				pw.write("0");
			}	
		}
		
		//房型管理 -- 查询所有房型
		@RequestMapping(value = "delectRoomTypeAll.do")
		public String delectRoomTypeAll(HttpServletRequest request,HttpServletResponse response,
				houseType housetype,Model model) throws UnsupportedEncodingException {
			
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			
			List<houseType> houselist = RoomtypeserviceImpl.queryHouseAll();
			
			model.addAttribute("houselist",houselist);
			
			return "view/System/roomTypemanagement";
		}
		
		//添加房型
		@RequestMapping(value = "addRoomtype.do")
		public void addRoomtype(HttpServletRequest request,HttpServletResponse response,
				houseType housetype) throws IOException {
			
			String roomtype = request.getParameter("housetype");
			String peple = request.getParameter("peple");
			String price = request.getParameter("price");
			String bed = request.getParameter("bed");
			String remarks = request.getParameter("remarks");
			
			housetype.setHousetype(roomtype);
			housetype.setGalleryful(Integer.parseInt(peple));
			housetype.setReserved1(price);
			housetype.setBednum(Integer.parseInt(bed));
			housetype.setRemark(remarks);
			housetype.setId(PrimaryKeyUtil.getPrimaryKey());
			
			List<houseType> houselist = RoomtypeserviceImpl.queryHouseAll();
			
			int j = 3;
			for(houseType one:houselist) {
				if(roomtype.equals(one.getHousetype())) {
					j = 2;
				}
			}
			
			PrintWriter pw = response.getWriter();
			
			if(j == 2) {
				pw.write("2");
			}else {
				int i = RoomtypeserviceImpl.insertSelective(housetype);
				if(i > 0) {
					pw.write("1");
				}else {
					pw.write("0");
				}
			}
				
		}
		
		//删除房型
		@RequestMapping(value = "deleteRoomType.do")
		public void deleteRoomType(HttpServletRequest request,HttpServletResponse response,
				houseType housetype) throws IOException {
			
			PrintWriter pw = response.getWriter();
			
			String id = request.getParameter("id");
			
			int i = RoomtypeserviceImpl.deleteByPrimaryKey(id);
			
			if(i > 0) {
				pw.write("1");
			}else {
				pw.write("0");
			}
			
		}
		
		//查询房型
		@RequestMapping(value = "selectRoomTypeByid.do")
		public void selectRoomTypeByid(HttpServletRequest request,HttpServletResponse response,
				houseType housetype) throws IOException {
			
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			
			String id = request.getParameter("id");
			
			houseType roomtype = RoomtypeserviceImpl.selectByPrimaryKey(id);
			
			ArrayList<houseType> s = new ArrayList();
			s.add(roomtype);
			
			//JSON格式返回数据
			String st = JSONArray.fromObject(s).toString();
			response.getWriter().write(st);	
			
		}
		
		//修改房型
		@RequestMapping(value = "amendRoomType.do")
		public void amendRoomType(HttpServletRequest request,HttpServletResponse response,
				houseType housetype) throws IOException {
			
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			
			PrintWriter pw = response.getWriter();
			
			String roomtype = request.getParameter("housetype");
			String id = request.getParameter("id");
			String peple = request.getParameter("peple");
			String bed = request.getParameter("bed");
			String price = request.getParameter("price");
			String remarks = request.getParameter("remarks");
			
			housetype.setHousetype(roomtype);
			housetype.setId(id);
			housetype.setGalleryful(Integer.parseInt(peple));
			housetype.setBednum(Integer.parseInt(bed));
			housetype.setReserved1(price);
			housetype.setRemark(remarks);
			
			int i = RoomtypeserviceImpl.updateByPrimaryKeySelective(housetype);
			
			if(i > 0) {
				pw.write("1");
			}else {
				pw.write("0");
			}
			
		}
		
		//脏房清洁
		@RequestMapping(value = "Dirtyroom.do")
		public void Dirtyroom(HttpServletRequest request,HttpServletResponse response,room rom) throws IOException {
			
			request.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=utf-8");
			
			PrintWriter pw = response.getWriter();
			
			String roomNumber = request.getParameter("roomNumber");
			rom.setRoomnum(Integer.parseInt(roomNumber));
			rom.setStatus("空闲");
			
			int i = RoomServiceImpl.Dirtyroom(rom);
			
			if(i > 0) {
				pw.write("1");
			}else {
				pw.write("0");
			}
			
		}
		
		
}
